package com.ohuang.test_compose

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.ohuang.test_compose.bean.Action
import com.ohuang.test_compose.bean.ActivityAction
import com.ohuang.test_compose.common.paging.simplePager
import com.ohuang.test_compose.composeView.PageSliceManager
import com.ohuang.test_compose.page.AlignmentLinePage
import com.ohuang.test_compose.page.AnimationPage
import com.ohuang.test_compose.page.CanvasPage
import com.ohuang.test_compose.page.ColumnKeyTestPage
import com.ohuang.test_compose.page.CompositionLocalPage
import com.ohuang.test_compose.page.ConstraintLayoutPage
import com.ohuang.test_compose.page.CustomLayoutPage
import com.ohuang.test_compose.page.CustomModifierPage
import com.ohuang.test_compose.page.EffectPage
import com.ohuang.test_compose.page.GesturePage
import com.ohuang.test_compose.page.ImagePage
import com.ohuang.test_compose.page.KeyboardOverlapsContent
import com.ohuang.test_compose.page.LazyColumnAndTextField
import com.ohuang.test_compose.page.NavControllerPage
import com.ohuang.test_compose.page.PageNavHostPage
import com.ohuang.test_compose.page.PermissionsPage
import com.ohuang.test_compose.page.RecombinationScopePage
import com.ohuang.test_compose.page.RemberTestPage
import com.ohuang.test_compose.page.StatePage
import com.ohuang.test_compose.page.TestWebViewPage
import com.ohuang.test_compose.page.TextPage
import com.ohuang.test_compose.test.TestPage
import com.ohuang.test_compose.page.TestHorizontalPager
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch

@ExperimentalPermissionsApi
@InternalComposeApi
@ExperimentalUnitApi
@ExperimentalMaterialApi
@ExperimentalAnimationApi
class MainViewModel : ViewModel() {
    var list = ArrayList<String>() //列表数据
    val halou = MutableLiveData<List<String>>() //列表数据观察者
    val select = MutableLiveData<Int>(0) //底部导航栏选中
    val actionList = ArrayList<Action>()// 界面跳转列表
    val pageSliceManager = PageSliceManager()
    var map:Map<String,String>?=null


    private val pager by lazy {
        simplePager {
            val data = ArrayList<String>()
            delay(1000)
            for (i in 0..9) {
                data.add("第 ${i + it * 10} item")
            }
            data
        }.cachedIn(viewModelScope)
    }
    var viewStates by mutableStateOf(StringViewState(pageData = pager))
        private set

    data class StringViewState(
        val pageData: Flow<PagingData<String>>,
        val isRefreshing: Boolean = false,
        val data: List<String> = emptyList(),
        val listState: LazyListState = LazyListState()
    )

    fun fetchData() {
        viewModelScope.launch {
            50.sp
            viewStates = viewStates.copy(isRefreshing = true)
            delay(1000)
            viewStates = viewStates.copy(isRefreshing = false)
        }
    }

    init {
        for (i in 1..10) {
            list.add("the $i item")
        }
        actionList.add(Action("page跳转") {
            page(it = 0)
        })
        actionList.add(Action("测试内容-testPage ") { TestPage() })
        actionList.add(Action("文字-TextPage") { TextPage() })
        actionList.add(Action("图片-ImagePage") { ImagePage() })
        actionList.add(Action("画布-CanvasPage") { CanvasPage() })
        actionList.add(Action("自定义布局-CustomLayoutPage") { CustomLayoutPage() })
        actionList.add(Action("动画-AnimationPage ") { AnimationPage() })
        actionList.add(Action("手势-GesturePage ") { GesturePage() })
        actionList.add(ActivityAction(name = "导航-NavControllerPage ") { NavControllerPage() })
        actionList.add(Action("ScrollableTabRow-testViewPage ") { TestHorizontalPager() })
        actionList.add(Action("权限-PermissionsPage ") { PermissionsPage() })
        actionList.add(ActivityAction(name = "webView-TestWebViewPage ") { TestWebViewPage() })
        actionList.add(Action("PageNavHostPage") {
            PageNavHostPage()
        })
        actionList.add(Action("RemberTestPage") {
            RemberTestPage()
        })
        actionList.add(Action("StatePage") {
            StatePage()
        })
        actionList.add(Action("ConstraintLayoutPage") {
            ConstraintLayoutPage()
        })
        actionList.add(Action("副作用 EffectPage") {
            EffectPage()
        })
        actionList.add(Action("对齐线 AlignmentLinePage"){
            AlignmentLinePage()
        })
        actionList.add(Action("CompositionLocal测试"){
            CompositionLocalPage()
        })
        actionList.add(Action("RecombinationScopePage 重组作用域"){
            RecombinationScopePage()
        })
        actionList.add(Action("CustomModifierPage 自定义Modifier"){
            CustomModifierPage()
        })
        actionList.add(Action("KeyboardOverlapsContent 键盘遮盖内容"){
            KeyboardOverlapsContent()
        })
        actionList.add(Action("LazyColumnAndTextField LazyColumn和TextField一起使用"){
            LazyColumnAndTextField()
        })
        actionList.add(Action("ColumnKeyTestPage  Column配合key一起使用可减少重组"){
            ColumnKeyTestPage()
        })
    }

    @Composable
    private fun page(it: Int) {
        Text(text = "is $it page",
            Modifier
                .height(50.dp)
                .fillMaxWidth()
                .clickable {
                    pageSliceManager.start { page(it = it + 1) }
                })
    }

    fun addFirstItem(string: String) {
        list.add(0, string)
        halou.postValue(list)
    }


}